//
//  ViewController.m
//  Nickname
//
//  Created by 汤文灿 on 15/9/18.
//  Copyright © 2015年 tcan. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
/**用于显示目前昵称*/
@property (weak, nonatomic) IBOutlet UILabel *currentNicknameLb;
/**用于显示随机生成的昵称*/
@property (weak, nonatomic) IBOutlet UITextField *randomNickname;
/**用于显示昵称使用状态*/
@property (weak, nonatomic) IBOutlet UILabel *showStatusLb;
/**形容词词库数据*/
@property(nonatomic,strong)NSMutableArray *adjectiveArray;
/**名词词库数据*/
@property(nonatomic,strong)NSMutableArray *nounsArray;
/**已被使用的昵称数据*/
@property(nonatomic,strong)NSMutableArray *alreadyUsedNameArray;
@end

@implementation ViewController


static  NSString *currentNickName=@"currentNickName";

#pragma mark--懒加载
-(NSArray *)adjectiveArray{
    if (!_adjectiveArray) {
        _adjectiveArray=[NSMutableArray array];
    }
    return _adjectiveArray;
}

-(NSMutableArray *)nounsArray{
    if (!_nounsArray) {
        _nounsArray=[NSMutableArray array];
    }
    return _nounsArray;
}

-(NSMutableArray *)alreadyUsedNameArray{
    if (!_alreadyUsedNameArray) {
        _alreadyUsedNameArray=[NSMutableArray array];
    }
    return _alreadyUsedNameArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //1、初始化词库数据
    for (int i=0; i<2; i++) {
        NSString *adjStr=[NSString stringWithFormat:@"%d",i];
        [self.adjectiveArray addObject:adjStr];
        
        NSString *nounStr=[NSString stringWithFormat:@"%d",i+100];
        [self.nounsArray addObject:nounStr];
    }
    //2、先产生一个随机的昵称显示
    [self randomCreate];
}

/**
 *  点击随机拼接生成昵称，不能生成已经存在的。
 */
- (IBAction)randomCreate {
    if (self.alreadyUsedNameArray.count==self.adjectiveArray.count*self.nounsArray.count) {
        self.showStatusLb.text=@"已无可用昵称...";
        return;
    }
    //获取形容词库数量
    NSInteger adjCount=self.adjectiveArray.count;
    //产生一个0～adjCount－1随机数
    NSInteger randomAdjNum=arc4random()%adjCount;
    //获得一个形容词adjStr
    NSString *adjStr=self.adjectiveArray[randomAdjNum];
    
    //获取名词词库数量
    NSInteger nonusCount=self.nounsArray.count;
    //产生一个0～nonusCount-1随机数
    NSInteger randomNonusNum=arc4random()%nonusCount;
    //获得一个名词nonusStr
    NSString *nonusStr=self.nounsArray[randomNonusNum];
    NSString *nickName=nil;
    nickName=[NSString stringWithFormat:@"%@的%@",adjStr,nonusStr];
    BOOL result=[self checkWithNickName:nickName];
    if (!result) {//未被使用
        self.randomNickname.text=nickName;
    }else{
        [self randomCreate];
    }
}

/**
 *  检查昵称是否已经存在（yes存在，no不存在）
 */
-(BOOL)checkWithNickName:(NSString *)nickName{
    return ([self.alreadyUsedNameArray indexOfObject:nickName] != NSNotFound);
}

/**
 *  确认使用该昵称
 */
- (IBAction)confirm2Use:(id)sender {
    NSString *name=self.randomNickname.text;
    if ([self checkWithNickName:name]) {
        self.showStatusLb.text=@"该昵称已被使用";
    }else{
        self.showStatusLb.text=[NSString stringWithFormat:@"修改成功，昵称为：%@",name];
        //把新修改的昵称加入已被使用的库
        [self.alreadyUsedNameArray addObject:name];
        self.currentNicknameLb.text=name;
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
